package com.cloudspace.jindun.message;

import android.os.Parcel;

import com.cloudspace.jindun.log.APPLog;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import io.rong.common.ParcelUtils;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

/**
 * Created by zengxianhua on 17/3/21.
 */

@MessageTag(value = "app:custom", flag = MessageTag.ISCOUNTED | MessageTag.ISPERSISTED)
public class InfoMessage extends MessageContent {
    public String content;
    public String title;
    public String read;
    public String id;

    public String groupId;

    public InfoMessage(String title, String content,
                       String read, String id, String groupId){
        this.title = title;
        this.content = content;
        this.read = read;
        this.id = id;
        this.groupId = groupId;
    }

    @Override
    public byte[] encode() {
        JSONObject jsonObj = new JSONObject();

        try {
            jsonObj.put("content", content);
            jsonObj.put("title", title);
            jsonObj.put("read", read);
            jsonObj.put("id", id);
            jsonObj.put("groupId", groupId);
        } catch (JSONException e) {
            APPLog.zxhLog("JSONException" + e.getMessage());
        }

        try {
            return jsonObj.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        ParcelUtils.writeToParcel(dest, content);//该类为工具类，对消息中属性进行序列化
        ParcelUtils.writeToParcel(dest, title);
        ParcelUtils.writeToParcel(dest, read);
        ParcelUtils.writeToParcel(dest, id);
        ParcelUtils.writeToParcel(dest, groupId);
    }

    public InfoMessage(byte[] data) {
        String jsonStr = null;

        try {
            jsonStr = new String(data, "UTF-8");
        } catch (UnsupportedEncodingException e1) {

        }

        try {
            JSONObject jsonObj = new JSONObject(jsonStr);

            if (jsonObj.has("content")) {
                content = jsonObj.optString("content");
            }

            if (jsonObj.has("title")){
                title = jsonObj.optString("title");
            }

            if (jsonObj.has("read")){
                read = jsonObj.optString("read");
            }

            if (jsonObj.has("id")){
                id = jsonObj.optString("id");
            }

            if (jsonObj.has("groupId")){
                id = jsonObj.optString("groupId");
            }
        } catch (JSONException e) {
            APPLog.zxhLog( "JSONException" + e.getMessage());
        }

    }

    //给消息赋值。
    public InfoMessage(Parcel in) {
        content= ParcelUtils.readFromParcel(in);//该类为工具类，消息属性
        //这里可继续增加你消息的属性
        title = ParcelUtils.readFromParcel(in);
        read = ParcelUtils.readFromParcel(in);
        id = ParcelUtils.readFromParcel(in);
        groupId = ParcelUtils.readFromParcel(in);
    }

    /**
     * 读取接口，目的是要从Parcel中构造一个实现了Parcelable的类的实例处理。
     */
    public static final Creator<InfoMessage> CREATOR = new Creator<InfoMessage>() {

        @Override
        public InfoMessage createFromParcel(Parcel source) {
            return new InfoMessage(source);
        }

        @Override
        public InfoMessage[] newArray(int size) {
            return new InfoMessage[size];
        }
    };

}
